package com.leesche.zcsp.node.web.controller.wechat.userinfo;

import com.leesche.zcsp.node.pay.wecha.data.Message;
import com.leesche.zcsp.node.pay.wecha.data.Oauth2To;
import com.leesche.zcsp.node.pay.wecha.service.WechatService;
import com.leesche.zcsp.node.pay.wecha.timer.WechatTimer;
import com.leesche.zcsp.node.pay.wecha.utils.SignUtil;
import com.leesche.zcsp.node.web.controller.pay.PayController;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017-01-10.
 */
@Controller
@RequestMapping("/wechat/userinfo")
public class WechatUserInfoController {
    public static final Logger LOGGER = Logger.getLogger(WechatUserInfoController.class);

    @Autowired
    private WechatService wechatService;
    @RequestMapping(value = {"getUserInfo"}, method = RequestMethod.GET)
    public String cmbcPayView(@RequestHeader("User-Agent") String userAgent,
                              String code,String url, HttpServletRequest request, ModelMap model) {
        LOGGER.info("code=" + code);
        Message<Oauth2To> message = wechatService.getOauth(code);
        if (message.getType() == Message.Type.error) {
            LOGGER.error("读取用户信息失败......");
            return "redirect:"+url+"?openid=";
        } else if (message.getType() == Message.Type.success) {
            return "redirect:"+url+"?openid="+message.getContent();
        }
        return "redirect:"+url+"?openid=";
    }


}
